// ====== class of 3 poles ======= // //<>//

// 10 positions of plate
final int[] PLATE_YPOS = {
  759, 727, 695, 663, 631, // plate [0, 4]
  599, 567, 535, 503, 471  // plate [5, 9]
};

class cpole {
  cpole(int x, int y, int poleNo) {
    colr = #967335;
    this.x = x;
    this.y = y;
    this.poleNo = poleNo;

    len = 230;
    thick = 25;
    halfthick = thick / 2;
    plateStack = new cplateStack();

    rangeCornerTLX = x - 250;
    rangeCornerTLY = 150;
    rangeConrerBRX = x + 250;
    rangeConrerBRY = 880;

    drawDragRange = false;
  }

  // all rect are drwan in rectMode(RADIUS);
  void paint() {
    noStroke();
    fill(colr);
    rect(x, 580, halfthick, y - 580, 5, 5, 5, 5); // pole (verticle)
    rect(x, y, len, thick, 5, 5, 5, 5); //  base (horizontal)
    textSize(40);
    text(poleNo + 1, x, y + 55); // poleNo 0,1,2 for toy name 1, 2, 3

    if (drawDragRange) {
      // draw the plate capture range
      stroke(#2CD14B, 80);
      noFill();
      rectMode(CORNERS);
      rect(rangeCornerTLX, rangeCornerTLY, rangeConrerBRX, rangeConrerBRY);
      rectMode(RADIUS);
      noStroke();
    }
  }

  int checkDragRange(int mx, int my) { // return the pole #
    if (mx > rangeCornerTLX && mx < rangeConrerBRX && my > rangeCornerTLY && my < rangeConrerBRY) {
      return poleNo;
    } else {
      return -2;
    }
  }

  void pushPlateIn(cplate plate) {
    // set x, y
    plate.x = x; // x is current pole's x
    plate.y = PLATE_YPOS[plateStack.size()]; // but y is getting higher and higher in screen

    // make it visible
    plate.visible = true;

    // push it into pole
    plateStack.push(plate);
  }

  void clearPlate() {
    plateStack.clear();
  }

  boolean isPoleEmpty() {
    return  plateStack.isEmpty();
  }

  int getPlatesCnt() {
    return plateStack.size();
  }

  int getTopPlateNo() {
    if (plateStack.size() == 0) {
      return -1; // if no plate, return -1
    } else {
      return plateStack.getTop().plateNo; // [0, 9]
    }
  }

  cplateStack plateStack; // store the plate stack on this pole

  private color colr;
  protected int x;
  private int y;
  private int len, thick, halfthick;
  protected int poleNo; // 0, 1, 2 for toy name 1 2 3

  // pole active range (plate capture range)
  // mouse drag in this rectanble range will caputre the plate of this pole
  protected int rangeCornerTLX, rangeConrerBRX; // TopLeft X, BottomRight X
  protected int rangeCornerTLY, rangeConrerBRY; // TopLeft Y, BottomRight Y

  boolean drawDragRange;
};

cpole pole0 = new cpole(260, 800, 0);
cpole pole1 = new cpole(800, 800, 1);
cpole pole2 = new cpole(1340, 800, 2);

cpole[] poles = {pole0, pole1, pole2};

void draw3Poles() {
  for (cpole i : poles) {
    i.paint();
  }
}

int checkPolesRange(int mx, int my) {
  for (cpole i : poles) {
    int rslt = i.checkDragRange(mx, my);
    if (rslt >= 0 ) { // 0, 1, 2 are plate #, not found is -2
      return rslt;
    }
  }
  return -2;
}
